<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Connect to an SSH server</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.ssh2-connect" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">ssh2_connect</h1>
  <p class="verinfo">(PECL ssh2 &gt;= 0.9.0)</p><p class="refpurpose"><span class="refname">ssh2_connect</span> &mdash; <span class="dc-title">Connect to an SSH server</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.ssh2-connect-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>ssh2_connect</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$host</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$port</code><span class="initializer"> = 22</span></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$methods</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$callbacks</code></span>
  ]]] ) : <span class="type">resource</span></div>

  <p class="para rdfs-comment">
   Establish a connection to a remote SSH server.
  </p>
  <p class="para">
   Once connected, the client should verify the server&#039;s hostkey using
   <span class="function"><a href="ssh2_fingerprint.html" class="function">ssh2_fingerprint()</a></span>, then authenticate using either
   password or public key.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.ssh2-connect-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">host</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">methods</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">methods</code> may be an associative array with up to four parameters
       as described below.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong><code class="parameter">methods</code> may be an associative array
               with any or all of the following parameters.</strong></caption>
        
         <thead>
          <tr>
           <th>Index</th>
           <th>Meaning</th>
           <th>Supported Values*</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>kex</td>
           <td>
            List of key exchange methods to advertise, comma separated
            in order of preference.
           </td>
           <td>
            <em>diffie-hellman-group1-sha1</em>,
            <em>diffie-hellman-group14-sha1</em>, and
            <em>diffie-hellman-group-exchange-sha1</em>
           </td>
          </tr>

          <tr>
           <td>hostkey</td>
           <td>
            List of hostkey methods to advertise, comma separated
            in order of preference.
           </td>
           <td>
            <em>ssh-rsa</em> and
            <em>ssh-dss</em>
           </td>
          </tr>

          <tr>
           <td>client_to_server</td>
           <td>
            Associative array containing crypt, compression, and
            message authentication code (MAC) method preferences
            for messages sent from client to server.
           </td>
           <td class="empty">&nbsp;</td>
          </tr>

          <tr>
           <td>server_to_client</td>
           <td>
            Associative array containing crypt, compression, and
            message authentication code (MAC) method preferences
            for messages sent from server to client.
           </td>
           <td class="empty">&nbsp;</td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       * - Supported Values are dependent on methods supported by underlying library.
       See <a href="http://libssh2.org/" class="link external">&raquo;&nbsp;libssh2</a> documentation for additional
       information.
      </p>
      <p class="para">
       <table class="doctable table">
        <caption><strong>
         <code class="parameter">client_to_server</code> and
         <code class="parameter">server_to_client</code> may be an associative array
         with any or all of the following parameters.
        </strong></caption>
        
         <thead>
          <tr>
           <th>Index</th>
           <th>Meaning</th>
           <th>Supported Values*</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>crypt</td>
           <td>List of crypto methods to advertise, comma separated
            in order of preference.</td>
           <td>
            <em>rijndael-cbc@lysator.liu.se</em>,
            <em>aes256-cbc</em>,
            <em>aes192-cbc</em>,
            <em>aes128-cbc</em>,
            <em>3des-cbc</em>,
            <em>blowfish-cbc</em>,
            <em>cast128-cbc</em>,
            <em>arcfour</em>, and
            <em>none**</em>
           </td>
          </tr>

          <tr>
           <td>comp</td>
           <td>List of compression methods to advertise, comma separated
            in order of preference.</td>
           <td>
            <em>zlib</em> and
            <em>none</em>
           </td>
          </tr>

          <tr>
           <td>mac</td>
           <td>List of MAC methods to advertise, comma separated
            in order of preference.</td>
           <td>
            <em>hmac-sha1</em>,
            <em>hmac-sha1-96</em>,
            <em>hmac-ripemd160</em>,
            <em>hmac-ripemd160@openssh.com</em>, and
            <em>none**</em>
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
      <p class="para">
       <blockquote class="note"><p><strong class="note">Note</strong>: 
        <strong>Crypt and MAC method &quot;<em>none</em>&quot;</strong><br />
        <p class="para">
         For security reasons, <em>none</em> is disabled by the underlying
         <a href="http://libssh2.org/" class="link external">&raquo;&nbsp;libssh2</a> library unless explicitly enabled
         during build time by using the appropriate ./configure options.  See documentation
         for the underlying library for more information.
        </p>
       </p></blockquote>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">callbacks</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">callbacks</code> may be an associative array with any
       or all of the following parameters.
       <table class="doctable table">
        <caption><strong>
         Callbacks parameters
        </strong></caption>
        
         <thead>
          <tr>
           <th>Index</th>
           <th>Meaning</th>
           <th>Prototype</th>
          </tr>

         </thead>

         <tbody class="tbody">
          <tr>
           <td>ignore</td>
           <td>
            Name of function to call when an
            <strong><code>SSH2_MSG_IGNORE</code></strong> packet is received
           </td>
           <td>void ignore_cb($message)</td>
          </tr>

          <tr>
           <td>debug</td>
           <td>
            Name of function to call when an
            <strong><code>SSH2_MSG_DEBUG</code></strong> packet is received
           </td>
           <td>void debug_cb($message, $language, $always_display)</td>
          </tr>

          <tr>
           <td>macerror</td>
           <td>
            Name of function to call when a packet is received but the
            message authentication code failed.  If the callback returns
            <strong><code>TRUE</code></strong>, the mismatch will be ignored, otherwise the connection
            will be terminated.
           </td>
           <td>bool macerror_cb($packet)</td>
          </tr>

          <tr>
           <td>disconnect</td>
           <td>
            Name of function to call when an
            <strong><code>SSH2_MSG_DISCONNECT</code></strong> packet is received
           </td>
           <td>void disconnect_cb($reason, $message, $language)</td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.ssh2-connect-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a resource on success, or <strong><code>FALSE</code></strong> on error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.ssh2-connect-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5615">
    <p><strong>Example #1 <span class="function"><strong>ssh2_connect()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     Open a connection forcing 3des-cbc when sending packets, any strength
     aes cipher when receiving packets, no compression in either direction,
     and Group1 key exchange.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Notify&nbsp;the&nbsp;user&nbsp;if&nbsp;the&nbsp;server&nbsp;terminates&nbsp;the&nbsp;connection&nbsp;*/<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">my_ssh_disconnect</span><span style="color: #007700">(</span><span style="color: #0000BB">$reason</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$language</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Server&nbsp;disconnected&nbsp;with&nbsp;reason&nbsp;code&nbsp;[%d]&nbsp;and&nbsp;message:&nbsp;%s\n"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$reason</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$message</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$methods&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'kex'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'diffie-hellman-group1-sha1'</span><span style="color: #007700">,<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'client_to_server'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'crypt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'3des-cbc'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'comp'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'none'</span><span style="color: #007700">),<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'server_to_client'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'crypt'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'aes256-cbc,aes192-cbc,aes128-cbc'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'comp'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'none'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$callbacks&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">'disconnect'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'my_ssh_disconnect'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$connection&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">ssh2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">'shell.example.com'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">22</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$methods</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$callbacks</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$connection</span><span style="color: #007700">)&nbsp;die(</span><span style="color: #DD0000">'Connection&nbsp;failed'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.ssh2-connect-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="ssh2_fingerprint.html" class="function" rel="rdfs-seeAlso">ssh2_fingerprint()</a> - Retrieve fingerprint of remote server</span></li>
    <li class="member"><span class="function"><a href="ssh2_auth_none.html" class="function" rel="rdfs-seeAlso">ssh2_auth_none()</a> - Authenticate as &quot;none&quot;</span></li>
    <li class="member"><span class="function"><a href="ssh2_auth_password.html" class="function" rel="rdfs-seeAlso">ssh2_auth_password()</a> - Authenticate over SSH using a plain password</span></li>
    <li class="member"><span class="function"><a href="ssh2_auth_pubkey_file.html" class="function" rel="rdfs-seeAlso">ssh2_auth_pubkey_file()</a> - Authenticate using a public key</span></li>
    <li class="member"><span class="function"><a href="ssh2_disconnect.html" class="function" rel="rdfs-seeAlso">ssh2_disconnect()</a> - Close a connection to a remote SSH server</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>